﻿using System;
using System.Collections.Generic;

namespace Chimp.Core
{
    public class IFNode
    {
        public Dictionary<string, string> Keys = new Dictionary<string, string>();
        public List<IFNode> Nodes = new List<IFNode>();

        public IFNode(string name, IFNode p)
        {
            if (p != null)
            {
                Parent = p;
                p.Nodes.Add(this);
            }
        }

        public IFNode Parent { get; set; }

        public string Name
        {
            get
            {
                if (Keys.ContainsKey("Name"))
                {
                    return Keys["Name"];
                }
                if (Keys.ContainsKey("UI"))
                {
                    return Keys["UI"];
                }
                return "";
            }
        }

        public string this[string index]
        {
            get
            {
                if (Keys.ContainsKey(index))
                {
                    return Keys[index];
                }
                return "";
            }
            set
            {
                if (Keys.ContainsKey(index))
                {
                    Keys.Remove(index);
                }
                Keys.Add(index, value);
            }
        }

        public int Int(string index)
        {
            return (int) Float(index);
        }

        public bool Bool(string index)
        {
            string v = this[index];

            if (v == "True")
            {
                return true;
            }
            else if (v == "False")
            {
                return false;
            }

            if (Float(index) >= 1.0f)
            {
                return true;
            }
            return false;
        }

        public bool Has(string index)
        {
            return Keys.ContainsKey(index);
        }

        public float Float(string index)
        {
            string v = this[index];
            if (v == "") return 0.0f;
            return float.Parse(v);
        }

        public void Debug(int x)
        {
            Console.Write("|");
            for (int i = 0; i < x; i++)
            {
                Console.Write("-");
            }
            foreach (string k in Keys.Keys)
            {
                Console.Write("(" + k + "=" + Keys[k] + ") ");
            }
            Console.WriteLine("|");
            foreach (IFNode n in Nodes)
            {
                n.Debug(x + 1);
            }
        }
    }
}